package io.zhengsh.juc._1lock.atomic.aba;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ABATest {

    private static AtomicInteger index = new AtomicInteger(10);

    public static void main(String[] args) {
        new Thread(()-> {
            index.compareAndSet(10, 101);
            index.compareAndSet(101, 10);
            System.out.println(Thread.currentThread() + ": 10->101->10");
        }, "Tom").start();

        new Thread(()-> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean result = index.compareAndSet(10, 1000);
            System.out.println(Thread.currentThread() + ": 更新结果：" + result + ", 更新后的值: " + index.get());
        }, "Jack").start();
    }
}
